module Explode.MovableBlock;
private import Explode.Stage;
private import Explode.Characters;
private import Graphics.IGraphics;
private import Input.GamePad;
private import System.Console;
private import std.math;
/**
*	動かせるブロック
*/
public class MovableBlock : BaseCharacter {

	public static const int WALKSPEED = 2;
	protected static int _IDCNT = 0;
	protected int _id ;
	public static int count(){ return _IDCNT;}

	protected GameStage	_stage;

	protected bool _fallFlag = false , _moveFlag = false;
	protected bool _movePower = 0;
	protected IGraphics _graphics;
	protected int _actionCnt ;
	protected string _textureName	 = "";
	protected int _xMoveCnt;
	protected int _hFace;
	protected string _cpu = "NORMAL";
	
	public void cpu( string v ) {
		_cpu = v;
	} //

	public this( GameStage stg , IGraphics g ) {
		_id = _IDCNT++;
		_stage = stg;
		_attribute  = Blocks.MBLOCK;
		_graphics = g;
		_cpu = "NORMAL";
		reset();
	} //

	public void reset() {
		if ( _sprite is null ) {
			_sprite 	= _graphics.getSprite();
		}
		if ( _textureName != _stage.mapTextureName ) {
			_textureName = _stage.mapTextureName;
			_texture = _graphics.createTexture( "MAP" , _stage.mapTextureName  , 0 , 0 );
			_texture.setFrameSize( 32 , 32 , 11 * 4 );
		}
		_x=0;	_y=0;	_z=115;
		_actionCnt	  = 0;
		_animCnt = _id;
		_alive	 = true;
		_cpu	 = "NORMAL";

		_texture.frameIndex = 11 * 4;
		_sprite.reset();
		_sprite.visible = true;
		_sprite.moveTo( _x , _y );
		_sprite.setSize( Blocks.WIDTH , Blocks.HEIGHT );
		_sprite.texture = _texture;
		_sprite.z = _z;
	} //


	public bool action() {
		_actionCnt++;
		int sk = _actionCnt % 14;

		sk = ( ( _actionCnt % 9 ) % 2 == 0 );

		_stage.subXYBlockMask( bx , by , Blocks.MBLOCK );
		if( !sk ) walkAction();
		_stage.addXYBlockMask( bx , by , Blocks.MBLOCK );
		return true;
	} //

	protected bool walkAction() {
		alias Blocks		B;
		alias B.WIDTH		bWidth;
		alias B.HEIGHT		bHeight;
		alias B.HALFWIDTH	hw;
		alias B.HALFHEIGHT	hh;
		int bx  = this.bx , by = this.by;
		int cb  = _stage.getXYBlock( bx , by );
		int fb  = _stage.getXYPointBlock( _x + hw , _y + bHeight );
		int fbb = _stage.getXYPointBlock( _x + hw,  _y + bHeight - 1 );
		int ctx = _x + hw , cty = _y + hh;
		int xs  = _x % bWidth , ys = _y % bHeight;

		// 落下判定
		_fallFlag = false;
		if ( xs == 0 && !B.isMatch( _stage.getXYPointBlock( ctx , _y + bHeight ), B.BLOCK | B.MBLOCK ) ) {
			_fallFlag = true;
		}

		int xv = 1 ;

		if ( xs == 0 && ys == 0 ) {
			cb = _stage.getXYBlock( bx , by );
			if ( _xMoveCnt > 0 ) {
				_xMoveCnt--;
				int xx  = ( ( _hFace > 0 ) ? 1 : -1 );
				int lrb = _stage.getXYBlock( bx + xx ,by );
				_xMove = xx * WALKSPEED;
				if ( B.isMatch( lrb , B.BLOCK | B.HHOLE ) ) {
						_xMoveCnt = _xMove = 0;
				} else
				if ( B.isMatch(lrb , B.ENEMY | B.MBLOCK ) && !_fallFlag && B.isFloor( fb ) ) {
					if ( !_stage.kickBlock( this , bx + xx , by , _xMoveCnt ) ) _xMoveCnt = _xMove = 0;
				}
			 }else
				_xMove = 0;
			 if( !B.isMatch( fb  , B.BLOCK | B.MBLOCK ) )	_fallFlag = true;
			 else											_fallFlag = false;
		}
		if ( _fallFlag ) {
			fbb = _stage.getXYPointBlock( _x + hw , _y + bHeight + WALKSPEED );
			if ( !B.isMatch( fbb , B.BLOCK | B.MBLOCK ) ) {
				_xMove = 0;
				_yMove = WALKSPEED;
			} else {
				_yMove = 0;
				_y = by * bHeight;
				_fallFlag = false;
			}
		} else
			_yMove = 0;
		moveBy( xMove , yMove );
		return true;
	} //
	

	public bool onCollided( ICharacter obj) {
		alias Blocks B;
		if ( obj.attribute == B.DIGGER ) {
			int obx =  obj.bx , oby = obj.by;
			int bx  = this.bx , by = this.by;
			if ( obx == bx && oby == by ) {
//				 soundSpriteManager.setImage(WaveTable.digger,1,0).moveTo( x , y );
				 _stage.subXYBlockMask( bx , by , B.MBLOCK );
				 kill();
			}
		} else
		if ( obj.attribute == B.PLAYER ) {
			return true;
		}
		return false;
	} //

	public bool draw( IGraphics g) {
		return true;
	} //
	public bool drawingEntry( IGraphics g ) {
		_sprite.moveTo( _x , _y );
		return _sprite.drawingEntry( g );
	} //

	public void moveTo( int x , int y ) {
		_x = x; _y = y;
		_sprite.moveTo( _x , _y );
	} //

	public bool onKicked( ICharacter obj , int pow = 1 ) {
		alias Blocks B;
		int hf  = obj.hFace;
		int bx  = this.bx , by = this.by;
		int hb  = _stage.getXYBlock( bx , by - 1 ) , fb = _stage.getXYBlock( bx , by + 1 );
		int lrb = _stage.getXYBlock( bx + hf , by );
		if ( _x % B.WIDTH != 0 ) return false;
		if ( B.isMatch( lrb , B.BLOCK | B.HHOLE | B.MBLOCK ) ) return false;
		if ( !B.isFloor( fb ) ) return false;
		if ( _fallFlag ) return false;
		if ( B.isMatch( lrb , B.ENEMY ) ) {
			if ( !_stage.kickBlock( obj , bx + hf , by , pow ) )return false;
		}
		
		_hFace = hf;
		_xMoveCnt=1;
		if ( pow >= 8 * 2 ) _xMoveCnt = 999;
		else
		if ( pow >  4 * 2 ) _xMoveCnt =   4;
		return true;
	} //


	public bool kill() {
		reset();
		_alive = false;
		return true;
	} //

} //
//
